//
//  LGTRWZCommenAlert.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/28.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZCommenAlert: LGTBaseVC {
    var titleLabel:UILabel? 
    var descLabel:UILabel?
    var subTextLabel:UILabel?
    var sureBtn:UIButton?
    var cancelBtn:UIButton?
    var titleStr:String = ""
    var descStr:String = ""
    var subTextStr:String = ""
    var sureTitleStr:String = ""
    var cancelTitleStr:String = ""
    var sureBlock:ClickBlock?
    var cancelBlock:ClickBlock?
    var sureCloseState:Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    static public func CommenAlertalertShow(title:String, desc:String, subText:String = "", sureTitle:String, cancelTitle:String, sureBlock:@escaping ClickBlock, cancelBlock:@escaping ClickBlock, sureCloseState:Bool = true, currentVC:UIViewController){
        let vc:LGTRWZCommenAlert = LGTRWZCommenAlert()
        vc.titleStr = title
        vc.sureTitleStr = sureTitle
        vc.cancelTitleStr = cancelTitle
        vc.descStr = desc
        vc.sureBlock = sureBlock
        vc.cancelBlock = cancelBlock
        vc.modalPresentationStyle = .overCurrentContext
        vc.sureCloseState = sureCloseState
        vc.subTextStr = subText
        currentVC.present(vc, animated: false, completion: nil)
    }
    
    override func setUI() {
        super.setUI()
        self.view.backgroundColor = BlackAlphaColor
        
        let backView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(10))
        backView.isUserInteractionEnabled = true
        backView.backgroundColor = UIColor.white
        self.view.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(self.view)
            make.width.equalTo(SizeFitFloat(303))
            make.height.equalTo(SizeFitFloat(298))
        }
        
        self.titleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(0), text: self.titleStr)
        self.titleLabel?.font = Font_Bold_18
        backView.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints({ (make) in
            make.height.equalTo(SizeFitFloat(19))
            make.top.equalTo(SizeFitFloat(22))
            make.centerX.equalTo(backView)
        })
        
        let closeBtn:LGTButton = LGTViewCreateUtil.share.createLGTButton(image: "close_icon", selected: "close_icon", dW: SizeFitFloat(10), dH: SizeFitFloat(10))
        backView.addSubview(closeBtn)
        closeBtn.tag = 2
        closeBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(SizeFitFloat(13))
            make.right.equalTo(-SizeFitFloat(23))
            make.top.equalTo(SizeFitFloat(20))
        }
        
        self.descLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: self.descStr)
        backView.addSubview(self.descLabel!)
        self.descLabel?.numberOfLines = 0
        self.descLabel?.snp.makeConstraints({ (make) in
            make.top.equalTo(SizeFitFloat(68))
            make.centerX.equalTo(backView)
            make.width.equalTo(SizeFitFloat(266))
        })
        
        if self.subTextStr.count > 0 {
            self.subTextLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: GrayColor, fontSize: SizeFitFloat(12), text: self.subTextStr)
            backView.addSubview(self.subTextLabel!)
            self.subTextLabel?.numberOfLines = 0
            self.subTextLabel?.snp.makeConstraints({ (make) in
                make.top.equalTo(self.descLabel!.snp_bottomMargin).offset(SizeFitFloat(27))
                make.centerX.equalTo(backView)
                make.width.equalTo(SizeFitFloat(266))
            })
        }
        
        let sureBtn:LGTButton = LGTViewCreateUtil.share.createTextLGTButton(title: self.sureTitleStr, font: Font_Normal_15, color: UIColor.white, selectedColor: UIColor.white, corner: SizeFitFloat(20), backColor: YellowColor)
        sureBtn.tag = 0
        backView.addSubview(sureBtn)
        sureBtn.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFitFloat(252))
            make.height.equalTo(SizeFitFloat(40))
            make.bottom.equalTo(-SizeFitFloat(64))
            make.centerX.equalTo(backView)
        }
        
        let cancelBtn:LGTButton = LGTViewCreateUtil.share.createTextLGTButton(title: self.cancelTitleStr, font: Font_Normal_15, color: YellowColor, selectedColor: YellowColor, corner: SizeFitFloat(20), backColor: UIColor.white)
        cancelBtn.tag = 1
        backView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFitFloat(252))
            make.height.equalTo(SizeFitFloat(40))
            make.bottom.equalTo(-SizeFitFloat(16))
            make.centerX.equalTo(backView)
        }
        
        cancelBtn.addTarget(self, action: #selector(clickAction), for: UIControl.Event.touchUpInside)
        closeBtn.addTarget(self, action: #selector(clickAction), for: UIControl.Event.touchUpInside)
        sureBtn.addTarget(self, action: #selector(clickAction), for: UIControl.Event.touchUpInside)
    }
    
    @objc func clickAction(sender:UIButton){
        if sender.tag == 1 || sender.tag == 2{
            if self.cancelBlock != nil{
                self.cancelBlock!()
            }
            self.dismiss(animated: false, completion: nil)
        }else{
            if self.sureBlock != nil{
                self.sureBlock!()
            }
            
            if self.sureCloseState == true{
                self.dismiss(animated: false, completion: nil)
            }
        }
        
    
        
//        weak var weakSelf = self
//        self.dismiss(animated: false) {
//            if sender.tag == 1 || sender.tag == 2{
//                if weakSelf?.cancelBlock != nil{
//                    weakSelf?.cancelBlock!()
//                }
//            }else{
//                if weakSelf?.sureBlock != nil{
//                    weakSelf?.sureBlock!()
//                }
//            }
//        }
    }
}
